<!DOCTYPE HTML>
<html class="butler">
<head>
    <title>科寓共享家</title>
    <% include common/meta.ejs %>
    <% include common/public.ejs %>
</head>
<body>
<div class="weui-pull-to-refresh-layer">
    <div class="pull-to-refresh-arrow"></div> <!-- 上下拉动的时候显示的箭头 -->
    <div class="pull-to-refresh-preloader"></div> <!-- 正在刷新的菊花 -->
    <div class="down">下拉刷新</div><!-- 下拉过程显示的文案 -->
    <div class="up">释放刷新</div><!-- 下拉超过50px显示的文案 -->
    <div class="refresh">正在刷新...</div><!-- 正在刷新时显示的文案 -->
</div>
<div class="weui-row weui-no-gutter">
    <div class="weui-col-50">
        <a href="butlerClean?status=1"
           class="topNav <%= data.properties.status=='1'?'active':'' %>">
            未处理
        </a>
    </div>
    <div class="weui-col-50">
        <a href="butlerClean?status=5"
           class="topNav <%= data.properties.status=='5'?'active':'' %>">
            已处理
        </a>
    </div>
</div>
<div id="doing"></div>
<div class="weui-infinite-scroll">
    <div class="infinite-preloader"></div><!-- 菊花 -->
    正在加载... <!-- 文案，可以自行修改 -->
</div>

<div id="editPopup" class="weui-popup-container  popup-bottom">
    <div class="weui-popup-overlay"></div>
    <div class="weui-popup-modal">
        <div class="toolbar">
            <div class="toolbar-inner">
                <a href="javascript:void(0);" class="picker-button" id="saveButton" style="z-index: 1000;">保存</a>
                <h1 class="title">保洁单</h1>
            </div>
        </div>
        <div class="modal-content">
            <form id="editForm">
                <div class="weui_cells weui_cells_form">
                    <div class="weui_cell">
                        <div class="weui_cell_hd"><label class="weui_label">费用合计</label></div>
                        <div class="weui_cell_bd weui_cell_primary">
                            <input class="weui_input" type="number" placeholder="本次保洁费用" name="cost"/>
                        </div>
                    </div>
                </div>
                <div class="weui_cells_title">处理结果</div>
                <div class="weui_cells weui_cells_form">
                    <div class="weui_cell">
                        <div class="weui_cell_bd weui_cell_primary">
                            <textarea class="weui_textarea" placeholder="保洁处理结果及备注" name="remark"></textarea>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<% include common/footer.ejs %>
<script>
    let loadFlag = false;
    let editId = '';
    let pageNumber = 1;

    let sendData = {

        id: '',
        result: ''
    }

    let statusData = {
        properties: {}
    };
    statusData.status = `<%= data.properties.status %>`;

    function reloadingData() {
        $('.weui-infinite-scroll').css('display', 'block');
        console.log(pageNumber);
        statusData.properties.pageSize = 10;
        statusData.properties.pageNumber = pageNumber;
        console.log(statusData);
        if (!loadFlag) {
            loadFlag = true;
            Dolphin.ajax({
                url: CTX + '/api/6890bc6b84ec46b6a0669d716fa41b68',
                type: 'post',
                data: Dolphin.json2string(statusData),
                forceUrl: 'true',
                onSuccess: function (data) {
                    loadFlag = false;
                    pageNumber++;
                    statusData.properties.pageNumber = pageNumber;
                    console.log(pageNumber);
                    let list = data.rows;
                    let contractId, con = '', con2 = '';
                    if (list.length == 0) {
                        $('.weui-infinite-scroll').css('display', 'none');
                        con = ` <br>
                    <div style="text-align: center">
                         <i class="weui_icon_success_circle"></i>到底啦~</div>`
                        $('#doing').append(con);
                        $(document.body).destroyInfinite()
                        return;
                    }

                    for (let i = 0; i < list.length; i++) {
                        if (list[i].status == 1) {
                            con = con +
                                `<div class="block">
    <h3>
        <span class="stateIcon"></span>
        ${list[i].statusName}
    </h3>
    <img src="/uploadFiles/clean/${list[i].mediaIds}.jpg" class="houseImg" />
    <p>
        <span class="label">清洁类型</span>
        <span>${list[i].type}</span>
    </p>
    <p>
        <span class="label">清洁时间</span>
        <span>${list[i].issueTime }</span>
    </p>
    <p>
        <span class="label">房间地址</span>
        <span>${list[i].address}</span>
    </p>
    <hr />
    <button class="editIcon" data-id="${list[i].id}"></button>
    <p>
      &nbsp
    </p>
</div>`
                        } else if (list[i].status == 5) {
                            con2 = con2 + `<div class="block">
    <h3>
        <span class="stateIcon"></span>
        ${list[i].statusName}
    </h3>
    <img src="/uploadFiles/clean/${list[i].mediaIds}.jpg" class="houseImg" />
    <p>
        <span class="label">清洁类型</span>
        <span>${list[i].type }</span>
    </p>
    <p>
        <span class="label">清洁时间</span>
        <span>${list[i].issueTime }</span>
    </p>
    <p>
        <span class="label">房间地址</span>
        <span>${list[i].address}</span>
    </p>



    <p>
        <span class="label">处理结果</span>
        <span>${list[i].result }</span>
    </p>
    <p>
        <span class="label">处理时间</span>
        <span>${list[i].handleTime }</span>
    </p>
    <p>
        <span class="label">处理人</span>
        <span>${list[i].handleUserName }</span>
    </p>
    <p>
        <span class="label">维修费用</span>
        <span>${list[i].cleanFee }</span>
    </p>
</div>`
                        }
                    }
                    if (statusData.status == '1') {
                        $('#doing').append(con);
                    } else {
                        $('#doing').append(con2)
                    }
                    ;
                    $('.editIcon').click(function () {
                        editId = $(this).data('id');
                        Dolphin.form.empty($('#editForm'));
                        $('#editPopup').popup();
                    });
                    $('#saveButton').click(function () {
                        let data = Dolphin.form.getValue('editForm');
                        $.closePopup();
                        sendData.id = editId;
                        sendData.result = data.remark;
                        sendData.cleanFee = data.cost;

                        Dolphin.ajax({
                            url: CTX + '/api/9598e452b51b49d4908610dbd717fe7f',
                            type: 'post',
                            data: Dolphin.json2string(sendData),
                            forceUrl: 'true',
                            onSuccess: function (resData) {

                                var userId = resData.value.issueUser;
                                var fee = resData.value.cleanFee;
                                var managerName = resData.value.handleUserName;
                                var managerPhone = resData.value.handleUserPhone;

                                Dolphin.ajax({
                                    url: CTX+'/api/5b90151b418f4ceb9469a6b265b0d617@id='+userId,
                                    type: Dolphin.requestMethod.GET,
                                    forceUrl:'true',
                                    onSuccess: function(reData){
                                        var openid = reData.value.weChat;
                                        //管理员处理完保洁后给客户发送报修账单信息
                                        var msgData = {
                                            "url" : "http://"+Dolphin.wechatUrl+"/yams/view/account?openId="+openid,
                                            "openid" : openid,
                                            "first" : "尊敬的客户，您好！您的保洁账单已出，请在12小时内完成支付",
                                            "keyword4" : fee,
                                            "keyword5" : '保洁',
                                            "remark" : "若有疑问，请联系管理员("+managerName+","+managerPhone+")，谢谢！"
                                        }
                                        Dolphin.ajax({
                                            url: '<%= global.config.contextPath == '/' ? '' : global.config.contextPath %>/wexin/sendBillMsg',
                                            type: Dolphin.requestMethod.POST,
                                            data: Dolphin.json2string(msgData),
                                            forceUrl: true,
                                            onSuccess: function () {
                                                $.toast("保存成功", function () {
                                                    location.reload();
                                                });
                                            }
                                        });
                                    }
                                });
                            }
                        });


//        Dolphin.ajax({
//            url:'/api/5cb78c416b674cab93f1c60f2c09d375',
//            data: Dolphin.json2string(data),
//            onSuccess: function (data) {
//                location.reload();
//            }
//        })
                    });
                }
            })
        }

    };

    function loadingData() {
        console.log(loadFlag);
        pageNumber = 1;
        statusData.properties.pageSize = 10;
        statusData.properties.pageNumber = 0;
        console.log(statusData);
        if (!loadFlag) {
            loadFlag = true;
            Dolphin.ajax({
                url: CTX + '/api/bea8b7c4b0b84e59973632c199d5ce30',
                type: 'post',
                data: Dolphin.json2string(statusData),
                forceUrl: 'true',
                onSuccess: function (data) {
                    console.log('hhaha');
                    loadFlag = false;
                    let list = data.rows;
                    let contractId, con = '', con2 = '';

                    for (let i = 0; i < list.length; i++) {
                        if (list[i].status == 1) {
                            con = con + `<div class="block">
    <h3>
        <span class="stateIcon"></span>
        ${list[i].statusName}
    </h3>
    <img src="/uploadFiles/clean/${list[i].mediaIds}.jpg" class="houseImg" />
    <p>
        <span class="label">清洁类型</span>
        <span>${list[i].type}</span>
    </p>
    <p>
        <span class="label">清洁时间</span>
        <span>${list[i].issueTime }</span>
    </p>
    <p>
        <span class="label">房间地址</span>
        <span>${list[i].address}</span>
    </p>
    <hr />
    <button class="editIcon" data-id="${list[i].id}"></button>
    <p>
      &nbsp
    </p>
</div>`
                        } else if (list[i].status == 5) {
                            con2 = con2 + `<div class="block">
    <h3>
        <span class="stateIcon"></span>
        ${list[i].statusName}
    </h3>
    <img src="/uploadFiles/clean/${list[i].mediaIds}.jpg" class="houseImg" />
    <p>
        <span class="label">清洁类型</span>
        <span>${list[i].type }</span>
    </p>
    <p>
        <span class="label">清洁时间</span>
        <span>${list[i].issueTime }</span>
    </p>
    <p>
        <span class="label">房间地址</span>
        <span>${list[i].address}</span>
    </p>



    <p>
        <span class="label">处理结果</span>
        <span>${list[i].result }</span>
    </p>
    <p>
        <span class="label">处理时间</span>
        <span>${list[i].handleTime }</span>
    </p>
    <p>
        <span class="label">处理人</span>
        <span>${list[i].handleUserName }</span>
    </p>
    <p>
        <span class="label">维修费用</span>
        <span>${list[i].cleanFee }</span>
    </p>
</div>`
                        }
                    }
                    ;
                    if (statusData.status == '1') {
                        $('#doing').html(con);
                        $(document.body).pullToRefreshDone();
                    } else {
                        $('#doing').html(con2);
                        $(document.body).pullToRefreshDone();
                    }
                    ;
                    $('.editIcon').click(function () {
                        editId = $(this).data('id');
                        Dolphin.form.empty($('#editForm'));
                        $('#editPopup').popup();
                    });
                    $('#saveButton').click(function () {
                        let data = Dolphin.form.getValue('editForm');
                        $.closePopup();
                        sendData.id = editId;
                        sendData.result = data.remark;
                        sendData.cleanFee = data.cost;

                        Dolphin.ajax({
                            url: CTX + '/api/9598e452b51b49d4908610dbd717fe7f',
                            type: 'post',
                            data: Dolphin.json2string(sendData),
                            forceUrl: 'true',
                            onSuccess: function (resData) {

                                var userId = resData.value.issueUser;
                                var fee = resData.value.cleanFee;
                                var managerName = resData.value.handleUserName;
                                var managerPhone = resData.value.handleUserPhone;

                                Dolphin.ajax({
                                    url: CTX+'/api/5b90151b418f4ceb9469a6b265b0d617@id='+userId,
                                    type: Dolphin.requestMethod.GET,
                                    forceUrl:'true',
                                    onSuccess: function(reData){
                                        var openid = reData.value.weChat;
                                        //管理员处理完保洁后给客户发送报修账单信息
                                        var msgData = {
                                            "url" : "http://"+Dolphin.wechatUrl+"/yams/view/account?openId="+openid,
                                            "openid" : openid,
                                            "first" : "尊敬的客户，您好！您的保洁账单已出，请在12小时内完成支付",
                                            "keyword4" : fee,
                                            "keyword5" : '保洁',
                                            "remark" : "若有疑问，请联系管理员("+managerName+","+managerPhone+")，谢谢！"
                                        }
                                        Dolphin.ajax({
                                            url: '<%= global.config.contextPath == '/' ? '' : global.config.contextPath %>/wexin/sendBillMsg',
                                            type: Dolphin.requestMethod.POST,
                                            data: Dolphin.json2string(msgData),
                                            forceUrl: true,
                                            onSuccess: function () {
                                                $.toast("保存成功", function () {
                                                    location.reload();
                                                });
                                            }
                                        });
                                    }
                                });
                            }
                        });


//        Dolphin.ajax({
//            url:'/api/5cb78c416b674cab93f1c60f2c09d375',
//            data: Dolphin.json2string(data),
//            onSuccess: function (data) {
//                location.reload();
//            }
//        })
                    });
                }
            })
        }

    };

    $(document).ready(
        function () {
            loadingData();
            console.log(`???`);
        }
    );
    $(document.body).pullToRefresh();

    $(document.body).on("pull-to-refresh", function () {
        //do something
        $('.weui-infinite-scroll').css('display', 'none');
        loadingData();
        $(document.body).infinite(100);

    });

    $('.weui-infinite-scroll').css('display', 'none');

    $(document.body).infinite(20);

    var loading = false;  //状态标记

    $(document.body).infinite().on("infinite", function () {
        $('.weui-infinite-scroll').css('display', 'block');
        if (loading) return;
        loading = true;
        setTimeout(function () {
            if (!loadFlag) {
                reloadingData();
            }
            loading = false;

        }, 1500);   //模拟延迟
    });


</script>
</body>
</html>